package TUI;

import ControllerLayer.*;
import ModelLayer.*;

import java.util.Scanner;
import java.util.ArrayList;
/**
 * This is a user interface and the menu for handling the addressbook.
 * 
 * @author Group 4, dm82
 * @version (a version number or a date)
 */
public class AddressBookUI
{
    private AddressCtr addressCtr;
    /**
     * Constructor for objects of class AddressBookUI
     */
    public AddressBookUI()
    {
        addressCtr = AddressCtr.getInstance();
    }
    
    /**
     * This method starts the address boook menu
     */
    public void start()
    {
        clr();
        addressMenu();
    }
    
    /**
     * This method hadles the choises made in ´the address menu
     */
    private void addressMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeAddressBookMenu();
            switch(choise)
            {
                case 1:
                    addEmployee();
                    break;
                case 2:
                    addCustomer();
                    break;
                case 3:
                    showPerson();
                    break;
                case 4:
                    deletePerson();
                    break;
                case 5:
                    showPersonList();
                    break;
                case 6:
                    exit = true;
                    break;
                default:
                    break;
            }
        }
    }
    
    private int writeAddressBookMenu()
    {
        space();
        print(" *** Addresse Kartotek Menu *** ");
        print(" (1) Tilfoej ny medarbejder");
        print(" (2) Tilfoej ny kunde");
        print(" (3) Vis detaljer om person");
        print(" (4) Slet person");
        print(" (5) Vis liste over personer");
        print(" (6) Gaa tilbage til hovedmenu");
        print("\n Foretag din beslutning: ");
            
        boolean exit = false;
        int choise = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                choise = keyboard.nextInt();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt valg, proev igen");
            }
        }
        return choise;
    }
    
    private String getInputString(String str)
    {
        print(str);
        
        boolean exit = false;
        String string = "";
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                string = keyboard.nextLine();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return string;
    }
    
    private int getInputInt(String str)
    {
        print(str);
        
        boolean exit = false;
        int integer = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                integer = keyboard.nextInt();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return integer;
    }
    
    private double getInputDouble(String str)
    {
        print(str);
        
        boolean exit = false;
        double dob = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                dob = keyboard.nextDouble();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return dob;
    }
    
    private long getInputLong(String str)
    {
        print(str);
        
        boolean exit = false;
        long lon = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                lon = keyboard.nextLong();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return lon;
    }
    
    private void printDetails(Person p)
    {
        boolean isCustomer = true;
        Customer c = null;
        space();
        try
        {
            c = (Customer)p;
        }
        catch(Exception e)
        {
            isCustomer = false;
        }
        print("\n");
        print("ID:      " + p.getId());
        print("Navn:    " + p.getName());
        print("Adresse: " + p.getAddress());
        print("Telefon nr.: " + p.getPhoneNumber());
        print("Email adr.;  " + p.getEmail());
        if(isCustomer)
        {
            print("Status:      Kunde");
            print("Kredit loft: " + c.getAccountLimit());
            print("Kredit balance:" + c.getAccountBalance());
        }
        else
        {
            print("Status:      Medarbejder");
        }
        space();
    }
    
    private void addEmployee()
    {
        String name = getInputString("Indtast medarbejderens navn: ");
        String address = getInputString("Indtast medarbejderens adresse:");
        String phone = getInputString("Indtast medarbejderens telefon nummer:");
        String email = getInputString("Indtast medarbejderens email adresse:");
        String password = getInputString("Indtast medarbejderens adgangskode:");
        space();
        
        addressCtr.createEmployee(name,address,phone,email,password);
    }
    
    private void addCustomer()
    {
        String name = getInputString("Indtast kundens navn: ");
        String address = getInputString("Indtast kundens adresse:");
        String phone = getInputString("Indtast kundens telefon nummer:");
        String email = getInputString("Indtast kundens email adresse:");
        double accountLimit = getInputDouble("Indtast kundens kredit loft:");
        double accountBalance = 0;
        space();
        
        addressCtr.createCustomer(name,address,phone,email,accountLimit,accountBalance);
    }
    
    private void showPerson()
    {
        long id = getInputLong("Indtast personens id:");
        printDetails(addressCtr.getPerson(id));
        space();
    }
    
    private void deletePerson()
    {
        long id = getInputLong("Indtast personens id:");
        addressCtr.deletePerson(id);
        space();
    }
    
    private void showPersonList()
    {
        space();
        ArrayList<Person> list = addressCtr.getPersonList();
        for(Person p : list)
        {
            print("ID: " + p.getId() + " Navn: " + p.getName() + " Status: " + getStatus(p));
        }
        space();
    }
    
    private String getStatus(Person p)
    {
        String status = "Kunde";
        try
        {
            Customer c = (Customer)p;
        }
        catch(Exception e)
        {
            status = "Medarbejder";
        }
        return status;
    }
    
    /**
     * This methid prints a sting to the terminal
     * 
     * @param   str A sting to be printed to the terminal.
     */
    private void print(String str)
    {
        System.out.println(str);
    }
    
    /**
     * This method clears the terminal.
     */
    private void clr()
    {
        print("\f");
    }
    
    /**
     * This method prints 2 new clear lines to make space
     */
    private void space()
    {
        print("\n");
    }
}